package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 签约合同Service业务层处理
 * 
 * @author alexis
 * @date 2025-11-01
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询签约合同
     * 
     * @param id 签约合同主键
     * @return 签约合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectById(id);
    }

    /**
     * 查询签约合同列表
     * 
     * @param contract 签约合同
     * @return 签约合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增签约合同
     * 
     * @param contract 签约合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return contractMapper.insert(contract);
    }

    /**
     * 修改签约合同
     * 
     * @param contract 签约合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除签约合同
     * 
     * @param ids 需要删除的签约合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除签约合同信息
     * 
     * @param id 签约合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteById(id);
    }

    @Override
    public void updateContractStatus() {

        //1. 查询订单状态 0 而且 开始时间 比现在时间 小的数据

        LambdaQueryWrapper<Contract> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Contract::getStatus,0).lt(Contract::getStartDate, LocalDateTime.now());

        List<Contract> contracts = list(wrapper);

        // 2.修改状态值
        contracts.forEach(e -> e.setStatus(1));


        //3. 更新操作
        updateBatchById(contracts);
    }
}
